/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.elka.gis.main.node;

/**
 *
 * @author looser
 */
public class Edge {
    private String from;
    private String to;
    private Integer color;
    private Node nFrom;
    private Node nTo;

    public Edge() {
    }

    public Edge(Node from, Node to) {
        this.from = from.getName();
        this.to = to.getName();
        this.color = null;
        this.nFrom = from;
        this.nTo = to;        
    }

    public Integer getColor() {
        return color;
    }

    public void setColor(Integer color) {
        this.color = color;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Edge other = (Edge) obj;
        //graf jest nieskierowany, więc krawędzie też są nieskierowane!
        //dlatego trzeba porównywać koniec z obydwoma końcami
        if ((this.from == null) ? (other.from != null) : !this.from.equals(other.from)) {
            if(!this.from.equals(other.to))
                return false;
        }
        if ((this.to == null) ? (other.to != null) : !this.to.equals(other.to)) {
            if(!this.to.equals(other.from))
                return false;
        }
        if (this.color != other.color && (this.color == null || !this.color.equals(other.color))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
    //jak equals zwraca true, to zwracane hashCody musza byc takie same, inaczej Set i Map przestanie dzialac
        
        int partHash = (this.from != null ? this.from.hashCode() : 0) + (this.to != null ? this.to.hashCode() : 0);
        int hash = 7;
        hash = 71 * hash + partHash;
        hash = 71 * hash + (this.color != null ? this.color.hashCode() : 0);
        return hash;
    }

    public boolean isColored() {
        if (this.color == null)
            return false;
        else
            return true;
    }

    /**
     * @return the nFrom
     */
    public Node getnFrom() {
        return nFrom;
    }

    /**
     * @return the nTo
     */
    public Node getnTo() {
        return nTo;
    }

    @Override
    public String toString() {
        String s = "";
        if (this.from!=null)
           s+=this.from;
        s+="->";
        if (this.to!=null)
           s+=this.to;
        if (this.color!=null)
           s+=" " + this.color;
        return s;
    }

}
